<template>
  <div class="mod-config">
    <h2>临时供应商注册</h2>
    <el-form class="flex-box" :label-position="labelPosition" :rules="rules"  :model="supplier">
      <el-form-item label="供应商编号:" prop="supplierNum">
        <el-input v-model="supplier.supplierNum"></el-input>
      </el-form-item>
      <el-form-item label="供应商证书编号:" prop="certificate">
        <el-input v-model="supplier.certificate"></el-input>
      </el-form-item>
      <el-form-item label="公司名称:" prop="company">
        <el-input v-model="supplier.company"></el-input>
      </el-form-item>
      <el-form-item label="公司性质:" prop="kind">
        <el-select v-model="supplier.kind" placeholder="请选择公司性质">
              <el-option  v-for="data in companyList" :label="data.desc" :value="data.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="联系人:" prop="contact">
        <el-input v-model="supplier.contact"></el-input>
      </el-form-item>
      <el-form-item label="电话:" prop="phone">
        <el-input v-model="supplier.phone"></el-input>
      </el-form-item>
      <el-form-item label="传真:" prop="fax">
        <el-input v-model="supplier.fax"></el-input>
      </el-form-item>
      <el-form-item label="电子邮件:" prop="email">
        <el-input v-model="supplier.email"></el-input>
      </el-form-item>
      <el-form-item label="地址:" prop="address">
        <el-input v-model="supplier.address"></el-input>
      </el-form-item>
      <el-form-item label="邮编:" prop="zip">
        <el-input v-model="supplier.zip"></el-input>
      </el-form-item>
      <el-form-item label="注册资金:" prop="regFund" >
        <el-input v-model="supplier.regFund" placeholder="元"></el-input>
      </el-form-item>
      <el-form-item label="法人代表:" prop="corporation">
        <el-input v-model="supplier.corporation"></el-input>
      </el-form-item>
      <el-form-item label="用户名称:" prop="userEntity.username">
        <el-input v-model="supplier.userEntity.username"></el-input>
      </el-form-item>
    </el-form>
    <p style="text-align: right; margin-right: 50px">
      <el-button @click="empty()">清空</el-button>
      <el-button @click="addSupplier()" v-if="isAuth('purchase:suppmaterial:save')" type="primary">新增</el-button>
    </p>
  </div>
</template>

<script>
  export default {
    data () {
      return {
        dataForm: {},
        dataListLoading: false,
        addOrUpdateVisible: false,
        labelPosition: 'left',
        supplier:{
          supplierNum:'',
          certificate:'',
          company:'',
          kind:'',
          contact:'',
          phone:'',
          fax:'',
          email:'',
          address:'',
          zip:'',
          regFund:'',
          corporation:'',
          achievement:'',
          userEntity:{
            username:''
          },
        },
        // 公司性质列表
        companyList:[],

        rules: {
          supplierNum : [
            {required: true, message: '请输入供应商编号', trigger: 'blur'},
          ],
          certificate: [
            {required: true, message: '请输入供应商证书编号', trigger: 'blur'},
          ],
          company : [
            {required: true, message: '请输入公司名称', trigger: 'blur'},
          ],
          kind : [
            {required: true, message: '请选择公司性质', trigger: 'blur'},
          ],
          contact : [
            {required: true, message: '请输入联系人', trigger: 'blur'},
          ],
          phone : [
            {pattern:'^[0-9]*$', required: true, message: '请输入规范的电话号码', trigger: 'blur'},
          ],
          fax : [
            {pattern:'^[0-9]*$',  required: true, message: '请输入规范的传真', trigger: 'blur'},
          ],
          email : [
            {type:'email',required: true, message: '请输入规范的邮箱地址', trigger: 'blur'},
          ],
          zip : [
            {pattern:'^[0-9]*$', required: true, message: '请输入规范的邮编', trigger: 'blur'},
          ],
          regFund : [
            {pattern:'^[0-9]*$', required: true, message: '请输入规范的注册资金', trigger: 'blur'},
          ],
          corporation : [
            {required: true, message: '请输入法人代表', trigger: 'blur'},
          ],
          address : [
            {required: true, message: '请输入地址', trigger: 'blur'},
          ],
          userEntity:{
            username: [
              {required: true, message: '请输入用户名称7', trigger: 'blur'},
            ]
          }

        }

      }
    },
    created () {
      this.getCompanyList();
    },
    methods: {
      // 清空表单数据
      empty () {
          this.supplier = {}
      },
      // 获取公司性质数据列表
      getCompanyList () {
        this.$http({
          url: this.$http.adornUrl('/purchase/supplier/companyList'),
          method: 'get',
        }).then((data) => {
          if (data && data.code === 200) {
            this.companyList = data.companyList
          } else {
            this.companyList = []
          }
        })
      },
      // 添加供应商
      addSupplier(){
        this.$http({
          url: this.$http.adornUrl('/purchase/supplier/saveSupplierAndUser'),
          method: 'post',
          data:{
            'supplierNum':this.supplier.supplierNum,
            'certificate':this.supplier.certificate,
            'company':this.supplier.company,
            'kind':this.supplier.kind,
            'contact':this.supplier.contact,
            'phone':this.supplier.phone,
            'fax':this.supplier.fax,
            'email':this.supplier.email,
            'address':this.supplier.address,
            'zip':this.supplier.zip,
            'regFund':this.supplier.regFund,
            'corporation':this.supplier.corporation,
            'achievement':this.supplier.achievement,
            'userEntity':this.supplier.userEntity,
          }
        }).then((data) => {
          if (data && data.code === 200) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.supplier = {}
                this.supplier.userEntity = {}
              }
            })
          } else {
            this.$message.error("操作失败")
          }
        })
      }
      }
  }
</script>
<style lang="scss" scoped>
.flex-box,.el-form{
  //弹性布局
  display: flex;
  //flex-direction属性决定主轴的方向（即项目的排列方向）。
  flex-direction: row;
  //flex-wrap属性定义，如果一条轴线排不下，如何换行。
  //wrap：换行，第一行在上方。
  flex-wrap:  wrap;
  //align-content属性定义了多根轴线的对齐方式。
  //flex-start：与交叉轴的起点对齐。
  align-content: flex-start;
  //justify-content属性定义了项目在主轴上的对齐方式。
  //space-between：两端对齐，项目之间的间隔都相等。
  justify-content: space-between;

}
.el-form-item{
  width: 240px;
}
.el-form-item{
  margin-right: 50px;
  margin-left: 50px;
}
.el-select{
  width: 240px;
}
</style>
